package com.stock_t.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.stock_t.entity.StockTTransaction;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Mapper
public interface StockTTransactionMapper extends BaseMapper<StockTTransaction> {

    /**
     * 根据股票代码查询做T的次数和成功率
     * @param stockCode 股票编码
     * @return 包含totalCount(总次数)、successCount(成功次数)、successRate(成功率)的Map
     */
    @Select("SELECT " +
            "COUNT(*) as totalCount, " +
            "SUM(CASE WHEN t_profit > 0 THEN 1 ELSE 0 END) as successCount, " +
            "ROUND(SUM(CASE WHEN t_profit > 0 THEN 1 ELSE 0 END) * 100.0 / COUNT(*), 2) as successRate " +
            "FROM stock_t_transaction " +
            "WHERE stock_code = #{stockCode}")
    Map<String, Object> selectTCountAndSuccessRate(@Param("stockCode") String stockCode);

    List<StockTTransaction> selectStockTTransactionList(StockTTransaction stockTTransaction);
    
    /**
     * 查询指定日期和股票编码的做T总收益
     * @param transactionDate 交易日期
     * @param stockCode 股票编码
     * @return 当日做T总收益
     */
    @Select("SELECT COALESCE(SUM(t_profit), 0) FROM stock_t_transaction WHERE transaction_date = #{transactionDate} AND stock_code = #{stockCode}")
    BigDecimal getTotalTProfitByDateAndStock(@Param("transactionDate") Date transactionDate, @Param("stockCode") String stockCode);

    @Select("SELECT COALESCE(SUM(t_profit), 0) FROM stock_t_transaction WHERE stock_code = #{stockCode}")
    BigDecimal getTotalTProfitByStockCode( @Param("stockCode") String stockCode);

    /**
     * 获取收益汇总
     */
    @Select("SELECT " +
            "COALESCE(SUM(t_profit), 0) as totalTProfit, " +
            "COALESCE(SUM(CASE WHEN DATE_FORMAT(created_time, '%Y-%m') = DATE_FORMAT(CURDATE(), '%Y-%m') THEN t_profit ELSE 0 END), 0) as currentMonthTProfit, " +
            "COALESCE(MAX(CASE WHEN DATE_FORMAT(created_time, '%Y-%m') = DATE_FORMAT(CURDATE(), '%Y-%m') THEN t_profit ELSE 0 END), 0) as maxMonthlyTProfit, " +
            "COALESCE(MAX(t_profit), 0) as maxTProfit, " +
            "COALESCE(MIN(t_profit), 0) as minTProfit " +
            "FROM stock_t_transaction WHERE stock_code = #{stockCode}")
    Map<String, Object> selectProfitSummary(@Param("stockCode") String stockCode);

    /**
     * 获取月度收益
     */
    @Select("SELECT DATE_FORMAT(created_time, '%Y-%m') as month, " +
            "COALESCE(SUM(t_profit), 0) as profit " +
            "FROM stock_t_transaction " +
            "WHERE stock_code = #{stockCode} " +
            "AND YEAR(created_time) = YEAR(CURDATE()) " +
            "GROUP BY DATE_FORMAT(created_time, '%Y-%m') " +
            "ORDER BY month ASC")
    List<Map<String, Object>> selectMonthlyProfits(@Param("stockCode") String stockCode);

    /**
     * 获取最大单月收益
     */
    @Select("SELECT COALESCE(MAX(monthly_profit), 0) FROM (" +
            "SELECT DATE_FORMAT(created_time, '%Y-%m') as month, SUM(t_profit) as monthly_profit " +
            "FROM stock_t_transaction WHERE stock_code = #{stockCode} " +
            "GROUP BY DATE_FORMAT(created_time, '%Y-%m')) as monthly_data")
    BigDecimal selectMaxMonthlyProfit(@Param("stockCode") String stockCode);

    /**
     * 根据股票代码查询所有T交易记录
     */
    @Select("SELECT * FROM stock_t_transaction WHERE stock_code = #{stockCode} ORDER BY created_time DESC")
    List<StockTTransaction> selectByStockCode(@Param("stockCode") String stockCode);

    /**
     * 获取当前月收益
     */
    @Select("SELECT COALESCE(SUM(t_profit), 0) FROM stock_t_transaction " +
            "WHERE stock_code = #{stockCode} AND DATE_FORMAT(created_time, '%Y-%m') = DATE_FORMAT(CURDATE(), '%Y-%m')")
    BigDecimal selectCurrentMonthProfit(@Param("stockCode") String stockCode);

    /**
     * 获取总收益
     */
    @Select("SELECT COALESCE(SUM(t_profit), 0) FROM stock_t_transaction WHERE stock_code = #{stockCode}")
    BigDecimal selectTotalProfit(@Param("stockCode") String stockCode);
}